#!/bin/bash
#################连接互联网或者使用光盘yum源##################
yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
##############################安装Nginx#############################


#创建www用户并给予权限
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

#定义nginx安装包文件路径
nginxPackage=nginx-1.8.0
#定义安装目录
installPath=/usr/local/nginx

#创建缺失目录
mkdir -p /var/tmp/nginx/scgi/ \
mkdir -p /var/tmp/nginx/uwsgi/ \
mkdir -p /var/tmp/nginx/fastcgi/ \
mkdir -p /var/tmp/nginx/proxy/ \
mkdir -p /var/tmp/nginx/client/ \
mkdir -p /var/logs/nginx/ \
mkdir -p /var/run/nginx/ \
mkdir -p ${nginxPackage}/conf/vhost \

#解压编译安装
tar zxvf ${nginxPackage}.tar.gz
cd ${nginxPackage}
./configure \
--user=www \
--group=www \
--prefix=${installPath} \
--sbin-path=${installPath}/sbin/nginx \
--conf-path=${installPath}/conf/nginx.conf \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_ssl_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi

make
make install

######################设置Nginx开机自启动#########################

echo "
mkdir -p /var/run/nginx/
${installPath}/sbin/nginx -c ${installPath}/conf/nginx.conf
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT
" >> /etc/rc.d/rc.local

chmod +x /etc/rc.d/rc.local

echo "
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
"

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT

echo "finish"

